package cn.iocoder.yudao.module.nginxui.service.httpserver;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpconfig.HttpConfigDO;
import cn.iocoder.yudao.module.nginxui.dal.mysql.httpconfig.HttpConfigMapper;
import cn.iocoder.yudao.module.nginxui.service.httpconfig.HttpConfigSyncService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.nginxui.controller.admin.httpserver.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httpserver.HttpServerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.nginxui.dal.mysql.httpserver.HttpServerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.nginxui.enums.ErrorCodeConstants.*;

/**
 * Nginx HTTP下的Server块配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HttpServerServiceImpl implements HttpServerService {

    @Resource
    private HttpServerMapper httpServerMapper;
    @Resource
    private HttpConfigMapper httpConfigMapper;
    @Resource
    private HttpConfigSyncService httpConfigSyncService;

    @Override
    @Transactional
    public Long createHttpServer(HttpServerSaveReqVO createReqVO) {
        // 1. 业务逻辑（校验HTTP配置存在性等）
        HttpServerDO server = BeanUtils.toBean(createReqVO, HttpServerDO.class);
        httpServerMapper.insert(server);

        // 2. 获取主配置ID（通过HTTP配置关联）
        HttpConfigDO httpConfig = httpConfigMapper.selectById(server.getHttpId());
        if (httpConfig != null) {
            httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
        }
        return server.getId();
    }

    @Override
    @Transactional
    public void updateHttpServer(HttpServerSaveReqVO updateReqVO) {
        // 1. 业务逻辑（校验、更新等）
        HttpServerDO server = validateHttpServerExists(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO, server);
        httpServerMapper.updateById(server);

        // 2. 同步到主配置
        HttpConfigDO httpConfig = httpConfigMapper.selectById(server.getHttpId());
        if (httpConfig != null) {
            httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
        }
    }

    @Override
    @Transactional
    public void deleteHttpServer(Long id) {
        // 1. 业务逻辑（校验、删除等）
        HttpServerDO server = validateHttpServerExists(id);
        httpServerMapper.deleteById(id);

        // 2. 同步到主配置
        HttpConfigDO httpConfig = httpConfigMapper.selectById(server.getHttpId());
        if (httpConfig != null) {
            httpConfigSyncService.syncToMainConfig(httpConfig.getConfigId());
        }
    }

    @Override
        public void deleteHttpServerListByIds(List<Long> ids) {
        // 删除
        httpServerMapper.deleteByIds(ids);
        }


    private HttpServerDO validateHttpServerExists(Long id) {

        HttpServerDO server = httpServerMapper.selectById(id);
        if (server == null) {
            throw exception(HTTP_SERVER_NOT_EXISTS);
        }
        return server;
    }

    @Override
    public HttpServerDO getHttpServer(Long id) {
        return httpServerMapper.selectById(id);
    }

    @Override
    public PageResult<HttpServerDO> getHttpServerPage(HttpServerPageReqVO pageReqVO) {
        return httpServerMapper.selectPage(pageReqVO);
    }

}